package cn.itsource.ymcc.producer;

import cn.itsource.ymcc.constant.VideoConstants;
import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.result.JsonResult;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MediaProducerImpl implements MediaProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    private boolean flag = false;
    /**
     * 异步推流、同步发送
     * @param mediaFile
     * @return
     */
    @Override
    public JsonResult asyncSend(MediaFile mediaFile) {
        log.info("正在推送视频推流的消息......");
        rocketMQTemplate.asyncSend(VideoConstants.TOPIC_COURSE_VIDEO_PUSH_TO_SRS + ":" + VideoConstants.TAGS_COURSE_VIDEO_PUSH_TO_SRS,
                JSON.toJSONString(mediaFile), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println("推送成功");
                        flag = true;
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        System.out.println("推流失败");
                        flag = false;
                    }
                });
        if (flag){
            return  JsonResult.success();
        }
        return  JsonResult.error("推流失败");
    }
}